package com.epfresh.api.widget;

import android.content.Context;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.epfresh.api.entity.RequestTag;
import com.epfresh.api.global.AppActivity;
import com.epfresh.api.ucrop.PhotoCore;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.assist.ImageSize;
import com.epfresh.api.universalimageloader.core.imageaware.ImageViewAware;
import com.epfresh.api.utils.FileUtil;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.api.views.dialog.ImageViewDialog;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.Images;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wzh on 16-7-23.
 */
public class PhotoAddGroup extends LinearLayout implements View.OnClickListener, PhotoCore.PhotoResult {

    private boolean isSelect ;
    private PhotoCore photoCore;
    private String filePath = FileUtil.getImgCacheDir() + "/" + "product_banner";

    protected int num = 3;
    PhotoUploadView photoViewAdd;

    ImageViewDialog imageViewDialog;

    public PhotoAddGroup(Context context) {
        super(context);
        init();
    }

    public PhotoAddGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PhotoAddGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public PhotoCore getPhotoCore() {
        return photoCore;
    }

    public void setPhotoCore(PhotoCore photoCore) {
        this.photoCore = photoCore;
//        if (photoCore != null){
//            photoCore.setOnDismissListener(new DialogInterface.OnDismissListener() {
//                @Override
//                public void onDismiss(DialogInterface dialog) {
//                    isSelect = false;
//                }
//            });
//        }
    }

    protected void init() {


        photoViewAdd = getPhotoView();
        photoViewAdd.findViewById(R.id.v_top).setVisibility(GONE);
        photoViewAdd.setTag(R.id.item_key_id, "add");

        photoViewAdd.setOnClickListener(this);
        addView(photoViewAdd);


    }


    public boolean isSelect() {
        return isSelect;
    }

    public void setSelect(boolean select) {
        isSelect = select;
    }

    protected PhotoUploadView getPhotoView() {
        PhotoUploadView pv = (PhotoUploadView) inflate(getContext(), R.layout.item_photo_view, null);
        int space = getResources().getDimensionPixelSize(R.dimen.item_padding_right);
        int w = (LocalDisplay.SCREEN_WIDTH_PIXELS - space * (num + 1)) / 3;
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(w, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.setMargins(space, 0, 0, 0);
        pv.setLayoutParams(lp);
        pv.findViewById(R.id.v_top).setOnClickListener(this);
        pv.setOnClickListener(this);
        pv.setTag(R.id.item_key_status, "normal");
        return pv;
    }

    public boolean isImageUploading(){
        for (int i=0;i<getChildCount()-1;i++){
            PhotoUploadView pv = (PhotoUploadView)getChildAt(i);
            boolean uploading = pv.isUploading();
            if (uploading){
                return true;
            }
        }

        return false;
    }
    public boolean isImageUploadFail(){
        for (int i=0;i<getChildCount()-1;i++){
            PhotoUploadView pv = (PhotoUploadView)getChildAt(i);
            boolean uploading = pv.isUploadSuccess();
            if (!uploading){
                return true;
            }
        }

        return false;
    }

    public List<Images> getImages(){
        List<Images> images = new ArrayList<>();
        for (int i=0;i<getChildCount()-1;i++){
            PhotoUploadView pv = (PhotoUploadView)getChildAt(i);
            String photoId = pv.getPhotoId();
            Images images1 = new Images();
            images1.setBanner(true);
            images1.setAttachmentId(photoId);
            images1.setFilePath("file://"+pv.getFilePath());
            if ( i==0){
                images1.setCover(true);
            }
            images.add(images1);
        }

        return images;
    }

    @Override
    public void onClick(View view) {
        if (view == photoViewAdd) {
            isSelect = true;
            String fp = filePath + "_" + System.currentTimeMillis();
            photoCore.showPhotoDialog(Uri.fromFile(new File(fp)),fp, false);
        } else if (view.getId() == R.id.v_top) {

            removeView((View) view.getParent());
        } else if ("normal".equals(view.getTag(R.id.item_key_status))) {
            PhotoUploadView pv = (PhotoUploadView) view;

            if (pv.isFail()) {
                for (int i = 0; i < getChildCount(); i++) {
                    if (getChildAt(i) == view) {
                        RequestTag tag = new RequestTag();
                        tag.tag = "upload";
                        pv.uploadImage((AppActivity) getContext(), pv.getFilePath(), tag);
                        break;
                    }
                }
            } else {//预览图片
                for (int i = 0; i < getChildCount() - 1; i++) {
                    if (getChildAt(i) == view) {
                        if (imageViewDialog == null)
                            imageViewDialog = new ImageViewDialog(getContext());

                        if (pv.getFilePath() != null && !"".equals(pv.getFilePath())) {
                            imageViewDialog.show("file://" + pv.getFilePath());
                        }else if (pv.getUrl() != null){
                            imageViewDialog.show(pv.getUrl());
                        }
                        break;
                    }

                }


            }
        }
    }

    public void addNetPhoto(String url, String attachmentId) {
        PhotoUploadView photoView = getPhotoView();
        addView(photoView, getChildCount() - 1);
        photoView.setPhotoId(attachmentId);
        photoView.setUrl(url);
        photoView.showImage();
        ImageSize imageSize = new ImageSize(200, 200);
        ImageLoader.getInstance().displayImage(url, new ImageViewAware(photoView.getImagView()), photoView.getImageOption(), imageSize, null, null);
    }
    public void addFilePhoto(String filePath, String attachmentId) {
        if (filePath == null ){
            return;
        }
        PhotoUploadView photoView = getPhotoView();
        addView(photoView, getChildCount() - 1);
        photoView.setPhotoId(attachmentId);

        photoView.setFilePath(filePath.replaceAll("file://", ""));
        photoView.showImage();
        ImageSize imageSize = new ImageSize(200, 200);
        ImageLoader.getInstance().displayImage(filePath, new ImageViewAware(photoView.getImagView()), photoView.getImageOption(), imageSize, null, null);
    }

    @Override
    public void onSuccess(String path) {
        isSelect = false;
        PhotoUploadView photoView = getPhotoView();
        addView(photoView, getChildCount() - 1);

        photoView.setFilePath(path.replaceAll("file://", ""));
        photoView.showImage();
        ImageSize imageSize = new ImageSize(200, 200);
        ImageLoader.getInstance().displayImage(path, new ImageViewAware(photoView.getImagView()), photoView.getImageOption(), imageSize, null, null);
        RequestTag tag = new RequestTag();
//        tag.action = currentPhotoUploadIndex;
        tag.tag = "upload";
        photoView.uploadImage((AppActivity) getContext(), photoView.getFilePath(), tag);
    }

    @Override
    public void onFail(String msg) {
        isSelect = false;
    }

    @Override
    public void onReupload() {
        isSelect = false;
    }


    public boolean isPreviewImageViewShowing() {
        return (imageViewDialog != null && imageViewDialog.isShowing()) ? true : false;
    }

    public void dismissImageView() {
        if (imageViewDialog != null) {
            imageViewDialog.dismiss();
        }
    }

}
